//
//  ECHeadView_MineCoures.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/8.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECHeadView_MineCoures.h"

@interface ECHeadView_MineCoures()

@property (nonatomic, strong) ECHeadView_MineCoures *baseview;

@end

@implementation ECHeadView_MineCoures

-(UIView *)createHeadViewWithFrame:(CGRect)frame andTitleArray:(NSArray *)titleArray{
    self.baseview = [[ECHeadView_MineCoures alloc]initWithFrame:frame];
    self.baseview.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < titleArray.count; i++) {
        ECUButton *btn = [ECUButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((kScreenWidth-(titleArray.count-1))/(titleArray.count) * i + i, 0, (kScreenWidth-(titleArray.count-1))/(titleArray.count), frame.size.height);
        [btn setTitle:titleArray[i] forState:0];
        btn.tag = i + 10;
        [btn addTarget:self action:@selector(btn_clcik:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseview addSubview:btn];
        if (i == 0) {
            [btn setTitleColor:[UIColor colorWithHexString:@"#FF6701"] forState:0];
        }else{
            [btn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
        }
    }
    
    for (int i = 1; i < titleArray.count; i++) {
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth-(titleArray.count -1))/(titleArray.count) * i + (i-1), frame.size.height/4, 1, frame.size.height/2)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#DCDCDC"];
        [self.baseview addSubview:lineView];
    }
    
    [self.baseview updateConstraintsIfNeeded];
    
    UIView * scrollLine = [[UIView alloc]init];
    [self.baseview addSubview:scrollLine];
    scrollLine.backgroundColor = [UIColor colorWithHexString:@"ff6600"];
    scrollLine.tag = 99;
    scrollLine.frame = CGRectMake((((kScreenWidth-(titleArray.count-1))/titleArray.count)-kProportion_Width(72))/2, self.baseview.mj_h-kProportion_Width(9), kProportion_Width(72), kProportion_Width(9));
    scrollLine.layer.masksToBounds = YES;
    scrollLine.layer.cornerRadius = 2;
    return self.baseview;
}

-(void)btn_clcik:(ECUButton *)btn{
    for (id view in btn.superview.subviews) {//获取所有对象
        if ([view isKindOfClass:[ECUButton class]]) {//获取所有按钮
            ECUButton *button = (ECUButton *)view;
            if (button.tag != btn.tag) {
                [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:0];
            }else{
                [btn setTitleColor:[UIColor colorWithHexString:@"#FF6701"] forState:0];
            }
        }
    }
    
    UILabel *viewDemo = (UILabel*)[btn.superview.superview viewWithTag:99];//获取底部线条
    [UIView animateWithDuration:0 animations:^{
        viewDemo.frame = CGRectMake((btn.mj_w-kProportion_Width(72))/2, btn.mj_h-kProportion_Width(9), kProportion_Width(72), kProportion_Width(9));
    } completion:^(BOOL finished) {//跨图层移动
        [viewDemo removeFromSuperview];
        [btn addSubview:viewDemo];
    }];
    
    NSNotification *notification =[NSNotification notificationWithName:@"btnClickNoti" object:nil userInfo:@{@"tag":@(btn.tag)}];
    //通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

-(void)changePageOffsetWithCurrent:(int)currentPage andBaseView:(ECHeadView_MineCoures *)view{
    ECUButton *btn = (ECUButton*)[view.superview viewWithTag:10+currentPage];//获取button
    
    [self btn_clcik:btn];
    
}


@end
